package bl.user;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.List;

import po.Po;
import po.SystemLogPo;
import po.UserPo;
import blservice.user.UserBLService;
import dbservice.history.SystemLogDBService;
import dbservice.user.UserDBService;
import vo.UserVo;
import vo.Vo;

public class UserBL extends UnicastRemoteObject implements UserBLService{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private UserDBService userDB;
	private SystemLogDBService log;
	
	public UserBL(UserDBService db,SystemLogDBService log)throws RemoteException{
		this.userDB=db;
		this.log=log;
	}
	
	public UserVo checkLogin(UserVo vo){		
		UserPo po=userDB.get(userV2P(vo));
		UserVo r=new UserVo();
		if(po.isSuccessed()){
			if(vo.getPassword().equals(po.getPassword())){
				r=userP2V(po);
				r.successed();
				log.add(new SystemLogPo(vo.getName(), "登陆", ""));
			}else{
				r.failed("密码输入错误");
			}
			
		}else{
			r.failed(po.getFailMessage());
		}
		return r;
	}
	
	private UserVo userP2V(UserPo po){
		UserVo r=new UserVo();
		if(po.isSuccessed()){
			r.successed();
			r.setName(po.getName());
			r.setUserType(po.getUserType());
			r.setAuthority(po.getAuthority());
			}else{
			r.failed(po.getFailMessage());
		}
		return r;
	}
	
	private UserPo userV2P(UserVo vo){
		UserPo po=new UserPo();
		po.setAuthority(vo.getAuthority());
		po.setName(vo.getName());
		po.setPassword(vo.getPassword());
		po.setUserType(vo.getUserType());
		return po;
	}
	
	public Vo addUser(UserVo vo){
		Po po=userDB.add(userV2P(vo));
		Vo r=new Vo();
		if(po.isSuccessed()){
			r.successed();
			log.add(new SystemLogPo(vo.getOperator(), "增加用户", vo.getName()+","+vo.getUserType()+","+vo.getAuthority()));
		}else{
			r.failed(po.getFailMessage());
		}
		return r;
	}
	
	public Vo deleteUser(UserVo vo){
		Po po=userDB.delete(userV2P(vo));
		Vo r=new Vo();
		if(po.isSuccessed()){
			r.successed();
			log.add(new SystemLogPo(vo.getOperator(), "删除用户", vo.getName()));
		}else{
			r.failed(po.getFailMessage());
		}
		return r;
	}
	
	public Vo changePassword(UserVo vo){
		Vo r=new Vo();		
		UserPo po=userDB.get(userV2P(vo));
		if(!po.isSuccessed()){
			r.failed(po.getFailMessage());
			return r;
		}
		if(po.getPassword().equals(vo.getPassword())){
			po.setPassword(vo.getNewPassword());
			Po po2=userDB.update(po);
			if(!po2.isSuccessed()){
				r.failed(po2.getFailMessage());
				return r;
			}else{
				r.successed();
				log.add(new SystemLogPo(vo.getOperator(), "修改密码", vo.getName()));
				return r;
			}
		}else{
			r.failed("原密码错误");
			return r;
		}
	}
	
	public Vo changeAuthority(UserVo vo){
		Po po=userDB.update(userV2P(vo));
		Vo r=new Vo();
		if(po.isSuccessed()){
			r.successed();
			log.add(new SystemLogPo(vo.getOperator(), "修改用户权限", vo.getName()+"to"+vo.getUserType().name()+","+vo.getAuthority().name()));
		}else{
			r.failed(po.getFailMessage());
		}
		return r;
	}
	
	public List<UserVo> getAll(){
		List<UserPo> po=userDB.getAll();
		List<UserVo> vo=new ArrayList<UserVo>();
		for(int i=0;i<po.size();i++){
			vo.add(userP2V(po.get(i)));
		}
		return vo;
	}
}
